package edu.thu.studio.ormdesigner.model.orm.table;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

import edu.thu.studio.ormdesigner.model.AbstractObjectModel;
import edu.thu.studio.ormdesigner.model.domain.Domain;

@XStreamAlias("column")
public class Column extends AbstractObjectModel {
    
    private static final long serialVersionUID = 6998985022414351214L;
    
    @XStreamAsAttribute
    private String domain;
    
    @XStreamAsAttribute
    @XStreamAlias("data-type")
    private String dataType;
    
    @XStreamAsAttribute
    private Integer length;
    
    // 精度
    @XStreamAsAttribute
    private Integer precision;
    
    // 不允许为空
    @XStreamAsAttribute
    private Boolean mandatory = new Boolean(true);
    
    // 主键
    @XStreamAsAttribute
    private Boolean primary = new Boolean(false);
    
    @XStreamOmitField()
    private Domain domainObject;
    
    public String getDataType() {
        return dataType;
    }
    
    public void setDataType(String dataType) {
        this.dataType = dataType;
    }
    
    public Integer getLength() {
        return length;
    }
    
    public void setLength(Integer length) {
        this.length = length;
    }
    
    public Integer getPrecision() {
        return precision;
    }
    
    public void setPrecision(Integer precision) {
        this.precision = precision;
    }
    
    public String getDomain() {
        return domain;
    }
    
    public void setDomain(String domain) {
        this.domain = domain;
    }
    
    public Boolean getMandatory() {
        return mandatory;
    }
    
    public void setMandatory(Boolean mandatory) {
        this.mandatory = mandatory;
    }
    
    public Boolean getPrimary() {
        return primary;
    }
    
    public void setPrimary(Boolean primary) {
        this.primary = primary;
    }
    
    public boolean hasDomain() {
        if (null != this.getDomain() && this.getDomain().length() > 0) {
            return true;
        }
        return false;
    }
    
    public String getDomainName(){
    	if (hasDomain()) {
            return this.domainObject.getName();
        }
    	return null;
    }
    
    public Domain getDomainObject() {
        return domainObject;
    }
    
    public void setDomainObject(Domain domainObject) {
        this.domainObject = domainObject;
    }
    
    public static void copyData(Column from, Column to) {
        to.setId(from.getId());
        to.setPrimary(from.getPrimary());
        to.setMandatory(from.getMandatory());
        
        to.setCode(from.getCode());
        to.setName(from.getName());
        to.setComment(from.getComment());
        
        if (from.hasDomain()) {
            to.setDomain(from.getDomain());
            to.setDomainObject(from.getDomainObject());
            
            to.setDataType(null);
            to.setLength(null);
            to.setPrecision(null);
        } else {
            to.setDomain(null);
            to.setDomainObject(null);
            
            to.setDataType(from.getDataType());
            to.setLength(from.getLength());
            to.setPrecision(from.getPrecision());
        }
    }
    
}
